//
//  Http.swift
//  icloud
//
//  Created by raymond on 2022/7/15.
//

import Foundation

func get(){
  // 创建一个会话,这个会话可以复用
  let session = URLSession(configuration: .default)
  let url = "http://127.0.0.1:3100/icloud"
  var urlRequest = URLRequest(url: URL(string: url)!)
  // 创建一个网络任务
  let task = session.dataTask(with: urlRequest){(data, resp, error) in
    do {
      let result = try JSONSerialization.jsonObject(with: data!, options: []) as! NSDictionary
      print(result)
    } catch {
      print("无法连接到服务器")
      return
    }
  }
}

func post() -> String {
  let session = URLSession(configuration: .default)
  let url = "http://127.0.0.1:3100/icloud"
  var request = URLRequest(url: URL(string: url)!)
  request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  request.httpMethod="POST"
  let postData = ["email":"123@165.com"];
  let postString = postData.compactMap({ (k,v)-> String in
    return "\(k)=\(v)"
  }).joined(separator:"&")
  request.httpBody=postString.data(using: .utf8)
  
  let task = session.dataTask(with: request){(data, resp, error) in
    let result = ""
    
    return result
  }
  return ""
}
